package com.neuedu.studentmanage.controller;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.studentmanage.entity.Class;
import com.neuedu.studentmanage.entity.OutRequest;
import com.neuedu.studentmanage.entity.Pager;
import com.neuedu.studentmanage.entity.Result;
import com.neuedu.studentmanage.entity.Student;
import com.neuedu.studentmanage.service.OutRequestService;

@Controller
public class OutRequestController {
	
	@Autowired
	private OutRequestService outRequestService;

	@GetMapping("outrequest/index")
	public String goTo() {
		return "student/outRequest/outrequest";
	}
	
	@PostMapping("outRequest/list")
	@ResponseBody
	public Pager<OutRequest> findList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows) {
		Pager<OutRequest> pager = new Pager<>(page, rows);
		List<OutRequest> outRequests = outRequestService.findListByPager(pager);
		Integer total = outRequestService.findTotalByPager(pager);

		pager.setRows(outRequests);
		pager.setTotal(total);

		return pager;
	}
	
	@RequestMapping("outRequest/add")
	@ResponseBody
	public Result doAdd(OutRequest outRequest,Integer stuId,Integer claId) {
		Student student = new Student();
		student.setStuId(stuId);
		outRequest.setStudent(student);
		
		Class class1 = new Class();
		class1.setClaId(claId);
		outRequest.setClassId(class1);
		Result result = outRequestService.add(outRequest);
		
		return result;
	}
	
	@RequestMapping("outRequest/edit")
	@ResponseBody
	public Result doEdit(OutRequest outRequest) {
		Result result = outRequestService.edit(outRequest);
		return result;
	}
	
	
	@RequestMapping("outRequest/delete")
	@ResponseBody
	public Result doDelete(Integer outId) {
		List<OutRequest> list = new ArrayList<OutRequest>();
		list.add(new OutRequest(outId));

		Result result = outRequestService.deleteByIds(list);
		return result;
	}
}
